package cn.itcast.eshop.controller;

import cn.itcast.eshop.common.entity.User;
import cn.itcast.eshop.common.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录，根据用户名，密码获取用户对象
     * 1.调用UserDAO获取用户列表数据
     * List<User> getEntityLIst() throws Exception;
     * 2.遍历用户列表，逐个与给定用户对象的用户名，密码进行匹配
     * 3.匹配成功则返回该用户对象，失败返回null
     * @param user 封装了用户名 密码的实体对象
     * @return 返回user对象，或者当用户名 密码错误时返回null
     * @throws Exception
     */

    @RequestMapping(value = "login",method = POST)
    @ResponseBody
    public User login(User user){
        try {
           return userService.login(user);

        }catch (Exception e){
            System.out.println(e);
            return user;
        }
    }
}
